package com.hys.ultrasound.business.adapters;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hys.ultrasound.R;
import com.hys.ultrasound.data.entity.Goods;
import com.lpjeremy.uimodule.recyclerview.CustomRecyclerView;
import com.lpjeremy.uimodule.recyclerview.adpter.RvBaseAdapter;
import com.lpjeremy.uimodule.recyclerview.adpter.RvViewHolder;
import com.lpjeremy.uimodule.recyclerview.interfaces.OnRvItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @desc:商品数据adapter
 * @date:2019/11/20 10:55
 * @auther:lp
 * @version:1.1.6
 */
public class GoodsListAdapter extends RvBaseAdapter<Goods> {
    /**
     * 点击的item下标
     */
    private int clickIndex = 0;

    private OnRvItemClickListener onRvItemClickListener;


    public GoodsListAdapter(Context mContext, List<Goods> list, CustomRecyclerView recyclerView, OnRvItemClickListener onRvItemClickListener) {
        super(mContext, list, recyclerView);
        this.onRvItemClickListener = onRvItemClickListener;
    }

    @NonNull
    @Override
    public RvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = getItemRootView(R.layout.item_good_layout, parent);
        return new GoodsItemViewHolder(view, onRvItemClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull RvViewHolder holder, final int position) {
        final GoodsItemViewHolder viewHolder = (GoodsItemViewHolder) holder;

        final Goods goods = getItem(position);

        if (goods != null) {

            viewHolder.mTxtJHNumber.setVisibility(position == 0 ? View.VISIBLE : View.GONE);
            viewHolder.mTxtJHNumber.setText("拣货单号:" + goods.getDjbh());

            viewHolder.mTxtName.setText(goods.getSpmch());
            viewHolder.mTxtCompany.setText(goods.getWldwname());
            viewHolder.mTxtSUser.setText(goods.getShouhr());
            String spec = "";
            if (!TextUtils.isEmpty(goods.getShpgg())) {
                spec = goods.getShpgg().trim();
            }
            viewHolder.mTxtSpecs.setText(spec);

            viewHolder.mTxtCurCount.setText(String.valueOf(goods.getYiwchbaozhshl()));
            viewHolder.mTxtCountTotal.setText(String.valueOf(goods.getBaozhshl()));


            viewHolder.mTxtCurNum.setText(String.valueOf(goods.getYiwchlingsshl()));
            viewHolder.mTxtNumTotal.setText(String.valueOf(goods.getLingsshl()));


            goods.setOperationType(0);

            if (clickIndex == position) {
                //设置选中色
//                viewHolder.mLeftView.setBackgroundResource(R.drawable.ff8f5b_no_right_radius);
                viewHolder.mBtnReSweep.setVisibility(View.VISIBLE);
                viewHolder.mBtnDetail.setVisibility(View.VISIBLE);


                if (viewHolder.mLayoutNum.getVisibility() == View.VISIBLE) {
                    viewHolder.mBtnLayoutLS.setBackgroundResource(R.drawable.btn_bg_radius);
                    //添加选择“零散数”的事件
//                    viewHolder.mBtnLayoutLS.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            goods.setOperationType(2);
//
//                            ScatteredDialog scatteredDialog = new ScatteredDialog(mContext, goods, null);
//                            scatteredDialog.show();
//                        }
//                    });
                }

            } else {
                viewHolder.mLeftView.setBackgroundColor(Color.TRANSPARENT);
                viewHolder.mBtnDetail.setVisibility(View.GONE);
                viewHolder.mBtnReSweep.setVisibility(View.GONE);

                viewHolder.mLayoutNum.setOnClickListener(null);
                viewHolder.mBtnLayoutLS.setBackground(null);
            }

        }

    }

    /**
     * 获取当前选中的商品
     *
     * @return
     */
    public Goods getCurrentSlectedGoods() {
        if (clickIndex >= 0) {
            return getItem(clickIndex);
        }
        return null;
    }

    public void setClickIndex(int clickIndex) {
        if (this.clickIndex != clickIndex) {
            this.clickIndex = clickIndex;
            notifyData();
        }
    }

    /**
     * 重扫，重置扫码商品信息
     *
     * @param djbh
     * @param djsn
     */
    public void reSweepGoodsData(String djbh, int djsn) {
        boolean bool = false;
        for (Goods goods : getDataList()) {
            if (TextUtils.equals(goods.getDjbh(), djbh) && goods.getDj_sn() == djsn) {
                goods.setYiwchbaozhshl(0);
                goods.setYiwchsl(0);
                goods.setYiwchlingsshl(0);
//                goods.getScanList().clear();
                bool = true;
                break;
            }
        }
        if (bool) {
            notifyData();
        }
    }

    class GoodsItemViewHolder extends RvViewHolder {
        @BindView(R.id.txtJHNumber)
        TextView mTxtJHNumber;
        @BindView(R.id.leftView)
        View mLeftView;
        @BindView(R.id.txtCompany)
        TextView mTxtCompany;
        @BindView(R.id.txtName)
        TextView mTxtName;
        @BindView(R.id.txtSpecs)
        TextView mTxtSpecs;
        @BindView(R.id.txtCurCount)
        TextView mTxtCurCount;
        @BindView(R.id.txtCountTotal)
        TextView mTxtCountTotal;
        @BindView(R.id.btnDetail)
        TextView mBtnDetail;
        @BindView(R.id.txtCurNum)
        TextView mTxtCurNum;
        @BindView(R.id.txtNumTotal)
        TextView mTxtNumTotal;
        @BindView(R.id.btnReSweep)
        TextView mBtnReSweep;
        @BindView(R.id.txtSUser)
        TextView mTxtSUser;

        @BindView(R.id.layoutCount)
        LinearLayout mLayoutCount;
        @BindView(R.id.layoutNum)
        LinearLayout mLayoutNum;
        @BindView(R.id.btnLayoutLS)
        LinearLayout mBtnLayoutLS;

        public GoodsItemViewHolder(View itemView, OnRvItemClickListener onItemClickListener) {
            super(itemView, onItemClickListener);
            if (itemView == null) return;
            ButterKnife.bind(this, itemView);
        }
    }
}
